<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改个人信息</title>

    <link rel="stylesheet" href="../../element/element.css">
    <script src="../../element/vue.js"></script>
    <script src="../../element/index.js"></script>
    <script src="../../js/axios.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }

        h1 {
            text-align: center;
            margin-bottom: 30px;
        }

        .el-form {
            width: 400px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .el-form-item {
            margin-bottom: 20px;
        }
        .wide-select .el-input__inner {
    width: 280px;
}

    </style>
</head>

<body>
    <div id="app">
        <h1>修改个人信息</h1>
        <el-form ref="form" :model="form" label-width="120px">
            <el-form-item label="用户ID:">
                <el-input v-model="form.userId" readonly></el-input>
            </el-form-item>
            <el-form-item label="用户名:">
                <el-input v-model="form.username" readonly></el-input>
            </el-form-item>
            <el-form-item label="账号:">
                <el-input v-model="form.account" readonly></el-input>
            </el-form-item>
            <el-form-item label="慈善积分:">
                <el-input v-model="form.charitableScore" readonly></el-input>
            </el-form-item>
            <el-form-item label="性别:">
                <el-select v-model="form.gender" placeholder="请选择" class="wide-select">
                    <el-option label="男" value="男"></el-option>
                    <el-option label="女" value="女"></el-option>
                </el-select>
            </el-form-item>
            
            <el-form-item label="年龄:">
                <el-input v-model="form.age" type="number"></el-input>
            </el-form-item>
            <el-form-item label="邮箱:">
                <el-input v-model="form.email" type="email"></el-input>
            </el-form-item>
            <el-form-item label="地址:">
                <el-input v-model="form.address"></el-input>
            </el-form-item>
            <el-form-item label="生日:" class="wide-select">
                <el-date-picker v-model="form.birthday" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="电话:">
                <el-input v-model="form.phone"></el-input>
            </el-form-item>
            <el-form-item label="银行信息:">
                <el-input v-model="form.bank"></el-input>
            </el-form-item>
            <el-form-item label="银行卡号:">
                <el-input v-model="form.bankCardNumber"></el-input>
            </el-form-item>
            <el-form-item label="上传头像">
                <el-upload action="" :show-file-list="false" :on-change="handleAvatarChange" :auto-upload="false">
                    <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">提交</el-button>
                <el-button @click="cancelSubmit">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                form: {
                    userId: '',
                    username: '',
                    account: '',
                    charitableScore: '',
                    gender: '',
                    age: '',
                    email: '',
                    address: '',
                    birthday: '',
                    phone: '',
                    bank: '',
                    bankCardNumber: ''
                }
            },
            methods: {
                //头像上传
                handleAvatarChange(file,filelist) {
                    const userId = this.form.userId;
                    const formData = new FormData();
                    formData.append('userId', userId);
                    formData.append('file', file.raw);
                    axios.post('http://localhost:8888/api/user/updateImage', formData)

                        .then(data => {
                            if (data.data.code === 0) {
                                this.$message.success('头像上传成功');
                                //更新头像显示
                                this.avatarUrl = data.data.data
                            } else {
                                this.$message.error(data.message);
                            }
                        })
                        .catch(error => {
                            this.$message.error('发生错误：' + error);
                        });
                },
                getUserInfo(userId) {
                    fetch('http://localhost:8888/api/user/getUserInfo?userId=' + userId, {
                        method: 'GET'
                    })
                        .then(response => response.json())
                        .then(data => {
                            if (data.code === 0) {
                                this.form = Object.assign(this.form, data.data);
                                this.form.account = localStorage.getItem('account');
                                this.form.username = localStorage.getItem('userName');
                                this.form.birthday = this.formatBirthday(this.form.birthday);
                            } else {
                                this.$message.error('个人信息获取失败：' + data.message);
                            }
                        })
                        .catch(error => {
                            this.$message.error('发生错误：' + error);
                        });
                },
                formatBirthday(birthday) {
                    var birthDate = new Date(birthday);
                    var year = birthDate.getFullYear();
                    var month = birthDate.getMonth() + 1;
                    var day = birthDate.getDate();
                    return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
                },
                onSubmit() {
                    fetch('http://localhost:8888/api/user/updateUserInfo', {
                        method: 'PUT',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify(this.form)
                    })
                        .then(response => response.json())
                        .then(data => {
                            if (data.code === 0) {
                                this.$message.success('个人信息收集成功');
                                // 确保在 DOM 更新后进行页面跳转
                                setTimeout(() => {
                                    window.location.href = 'userInfoCenter.html';
                                }, 300);
                            } else {
                                this.$message.error('个人信息收集失败，请联系管理员');
                            }
                        })
                        .catch(error => {
                            this.$message.error('发生错误：' + error);
                        });
                },
                cancelSubmit() {
                    window.location.href = 'userInfoCenter.html';
                }
            },
            mounted() {
                var userId = localStorage.getItem('userId');
                this.getUserInfo(userId);
            }
        });
    </script>
</body>

</html>